package net.hualisheng.netty.mq;

import com.rabbitmq.client.Channel;
import net.hualisheng.netty.service.GrabRedPackService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.beans.factory.annotation.Autowired;

public class RabbitConsumer implements ChannelAwareMessageListener {

    private static final Logger looger= LoggerFactory.getLogger(RabbitConsumer.class);

    @Autowired
    private GrabRedPackService grabRedPackService;

    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        long tag = message.getMessageProperties().getDeliveryTag();
        try {
            String content = new String(message.getBody(), "UTF-8");
            looger.info("监听器已监测到"+content+"抢红包行为");
            grabRedPackService.gradRedPack(content);

            channel.basicAck(tag,false);


        }catch (Exception e){
            looger.error("抢红包作业操作发生异常:"+e.getMessage());
            channel.basicReject(tag,false);
        }
    }
}
